package com.slipper.system.sequence.controller;

import com.google.gson.reflect.TypeToken;
import com.slipper.core.gson.GsonUtil;
import com.slipper.util.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.slipper.system.sequence.bo.SequenceBO;
import com.slipper.system.sequence.service.SequenceService;

import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/system/sequence")
public class SequenceController {
    private static final Logger logger = LoggerFactory.getLogger(SequenceController.class);

    @Resource
    private SequenceService sequenceService;
    
    @PostMapping(value = "/queryBy")
    public String queryBy(@RequestBody SequenceBO bo) {
        if(Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("数据获取失败！");
        }
        return ResultUtil.successResult(sequenceService.queryBy(bo.getId()));
    }
    
    @PostMapping(value = "/queryList")
    public String queryList(@RequestBody SequenceBO bo) {
        return ResultUtil.successResult(sequenceService.queryList(bo));
    }

    @PostMapping(value = "/queryPage")
    public String queryPage(@RequestBody SequenceBO bo) {
        return ResultUtil.successResult(sequenceService.queryPage(bo));
    }

    @PostMapping(value = "/save")
    public String save(@RequestBody SequenceBO bo) {
        return ResultUtil.saveResult(sequenceService.save(bo));
    }

    @PostMapping(value = "/delete")
    public String delete(@RequestBody SequenceBO bo) {
        return ResultUtil.deleteResult(sequenceService.delete(bo));
    }

    @PostMapping(value = "/exportData")
    public void exportData(HttpServletResponse response, @RequestBody SequenceBO bo) throws IOException {
        if (bo.getIds() == null || bo.getIds().length == 0) {
            ResultUtil.failureResult(response, 400, "参数不正确！");
            return;
        }

        SequenceBO sequenceQO = new SequenceBO();
        sequenceQO.setIds(bo.getIds());
        List<SequenceBO> sequenceList = sequenceService.queryList(sequenceQO);
        String fileName = "sequence-" + DateUtil.format(new Date(), "dd-HH-mm-ss") + ".dat";
        FileUtil.downloadContent(response, fileName, AESUtil.encrypt(GsonUtil.toJson(sequenceList)));
    }

    @PostMapping(value = "/importData")
    public String importData(SequenceBO bo) {
        if (bo.getFile() == null) {
            return ResultUtil.failureResult("请选择上传文件！");
        }

        try {
            String content = new String(bo.getFile().getBytes(), StandardCharsets.UTF_8);
            Type type = new TypeToken<List<SequenceBO>>(){}.getType();
            List<SequenceBO> sequenceList = GsonUtil.fromJson(AESUtil.decrypt(content), type);
            sequenceService.saveList(sequenceList);
            return ResultUtil.successResult("导入序号成功。");
        } catch (Exception e) {
            logger.error("文件解释出错！", e);
            return ResultUtil.failureResult("文件解释出错！");
        }
    }
}
